/**
 * @author chenz
 */

websitenotes.createWebsiteNotesTable = function() {
	try {
		var statement = mDBConn.createStatement("CREATE TABLE IF NOT EXISTS notes (uuid VARCHAR(40), url TEXT, noteObj TEXT, noteText TEXT, PRIMARY KEY(uuid));");
		statement.execute();
	}	catch (e) {	alert (e); } finally { statement.reset();	}
}

websitenotes.setNoteInLocalDatabase = function(noteUuid, noteUrl, noteJSONObj, noteText) {
	try {
		var statement = mDBConn.createStatement("REPLACE INTO notes (uuid, url, noteObj, noteText) VALUES (?1, ?2, ?3, ?4);");
		statement.bindUTF8StringParameter(0, noteUuid);
		statement.bindUTF8StringParameter(1, noteUrl);
		statement.bindUTF8StringParameter(2, noteJSONObj);
		statement.bindUTF8StringParameter(3, noteText);
		statement.execute();
	}	catch (e) {	alert (e); } finally { statement.reset();	}
}

websitenotes.setNoteTextInLocalDatabase = function(noteUuid, noteText) {
	//alert("setNoteTextInLocalDatabase sees noteText as \""+noteText+"\" for textarea-"+noteUuid);
	try {
		var statement = mDBConn.createStatement("UPDATE notes SET noteText=?2 WHERE uuid=?1;");
		statement.bindUTF8StringParameter(0, noteUuid);
		statement.bindUTF8StringParameter(1, noteText);
		statement.execute();
	}	catch (e) {	alert (e); } finally { statement.reset();	}
}

websitenotes.setNoteJSONObjInLocalDatabase = function(noteUuid, noteJSONObj) {
	try {
		var statement = mDBConn.createStatement("UPDATE notes SET noteObj=?2 WHERE uuid=?1;");
		statement.bindUTF8StringParameter(0, noteUuid);
		statement.bindUTF8StringParameter(1, noteJSONObj);
		statement.execute();
	}	catch (e) {	alert (e); } finally { statement.reset();	}
}
	
websitenotes.getNotesFromLocalDatabase = function() {
	var retVal; 
	try {
		var statement = mDBConn.createStatement("SELECT uuid, url, noteObj, noteText FROM notes;");
		while (statement.executeStep()) {
			var uuid, url, noteObj, noteText;
			var cols = statement.columnCount; 
			for (var i = 0; i < cols; i++) {
				if (statement.getColumnName(i) == "uuid") uuid = statement.getString(i); 
				else if (statement.getColumnName(i) == "url") url = statement.getString(i); 
				else if (statement.getColumnName(i) == "noteObj") noteObj = statement.getString(i); 
				else if (statement.getColumnName(i) == "noteText") noteText = statement.getString(i); 
			}
			alert("UUID: "+uuid+"\nURL: "+url+"\nnoteObj: "+noteObj+"\nnoteText: "+noteText+"\n");
		}
		alert ("No more results.");
	}	catch (e) {	alert (e); } finally { statement.reset(); }
	return retVal;
}

websitenotes.getNoteFromLocalDatabase = function(uuid) {
	var retVal; 
	try {
		var statement = mDBConn.createStatement("SELECT uuid, url, noteObj, noteText FROM notes WHERE uuid=?1;");
		statement.bindUTF8StringParameter(0, uuid);
		while (statement.executeStep()) {
			var uuid, url, noteObj, noteText;
			var cols = statement.columnCount; 
			for (var i = 0; i < cols; i++) {
				if (statement.getColumnName(i) == "uuid") uuid = statement.getString(i); 
				else if (statement.getColumnName(i) == "url") url = statement.getString(i); 
				else if (statement.getColumnName(i) == "noteObj") noteObj = statement.getString(i); 
				else if (statement.getColumnName(i) == "noteText") noteText = statement.getString(i); 
			}
			alert("UUID: "+uuid+"\nURL: "+url+"\nnoteObj: "+noteObj+"\nnoteText: "+noteText+"\n");
		}
		alert ("No more results.");
	}	catch (e) {	alert (e); } finally { statement.reset(); }
	return retVal;
}

websitenotes.getNoteJSONObjFromLocalDatabase = function(uuid) {
	var retVal; 
	try {
		var statement = mDBConn.createStatement("SELECT uuid, url, noteObj, noteText FROM notes WHERE uuid=?1;");
		statement.bindUTF8StringParameter(0, uuid);
		while (statement.executeStep()) {
			var uuid, url, noteObj, noteText;
			var cols = statement.columnCount; 
			for (var i = 0; i < cols; i++) {
				if (statement.getColumnName(i) == "noteObj") retVal = statement.getString(i); 
			}
		}
	}	catch (e) {	alert (e); } finally { statement.reset(); }
	return retVal;
}


websitenotes.getNotesFromLocalDatabaseForURL = function(url) {
	var retArray = new Array();
	try {
		var statement = mDBConn.createStatement("SELECT uuid, url, noteObj, noteText FROM notes WHERE url=?1;");
		statement.bindUTF8StringParameter(0, url);
		while (statement.executeStep()) {
			var obj = new Object();
			var cols = statement.columnCount; 
			for (var i = 0; i < cols; i++) {
				if (statement.getColumnName(i) == "uuid") obj.uuid = statement.getString(i); 
				else if (statement.getColumnName(i) == "url") obj.url = statement.getString(i); 
				else if (statement.getColumnName(i) == "noteObj") obj.noteObj = statement.getString(i); 
				else if (statement.getColumnName(i) == "noteText") obj.noteText = statement.getString(i); 
			}
			retArray.push(obj);
		}
	}	catch (e) {	alert (e); } finally { statement.reset(); }
	return retArray;
}

websitenotes.removeNoteFromLocalDatabase = function(uuid) {
	try {
		var statement = mDBConn.createStatement("DELETE FROM notes WHERE uuid=?1;");
		statement.bindUTF8StringParameter(0, uuid);
		statement.execute();
	}	catch (e) {	/* alert (e); */ } finally { statement.reset(); }
}

websitenotes.eraseAllNotes = function(uuid) {
	try {
		var statement = mDBConn.createStatement("DELETE FROM notes;");
		statement.execute();
	}	catch (e) {	alert (e); } finally { statement.reset(); }
}

/* For Fetching */
/*

 */



/* AJAX Code For Future Remote Persistant Data */
/*
function makeRequest(url) {
	var httpRequest;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}

	if (!httpRequest) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');
}

function alertContents(httpRequest) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			alert(httpRequest.responseText);
		} else {
			alert('There was a problem with the request.');
		}
	}
}
*/